const util = require('../../utils/util.js');
const api = require('../../config/api.js');

Page({
  data: {
    id:'',
    name: '',
    mobile: "",
    activityId:'',
    activityTitle:'活动签到',
    disabled:false,
  },
  onLoad (options) {
    console.warn(options);
    if(!options.scene)return
    try{
      // const index = options.scene.indexOf('-')
      // const id = options.scene.substring(0,index);
      // const title = options.scene.substring(index+1);
      this.setData({
        activityId: options.scene,
        // activityTitle: decodeURIComponent(title)
      });
    }catch(e){
      // console.log(e)
    }

  },
  onShareAppMessage () {
    return {};
  },
  onSubmit () {
    const {name, activityId, activityTitle} = this.data;
    if(!name||!name.trim()){
      wx.showToast({
        title: '请填写姓名',
        icon: 'error',
        duration: 600
      })
      return
    }
    util.request(api.ActivitySign, {name, activityId,activityTitle}, 'post')
      .then(res => {
        if (res.errno === 200) {
          this.setData({
            disabled: true
          })
          wx.showModal({
            title: activityTitle,
            confirmColor: '#b4282d',
            content: '活动签到成功',
            showCancel:false,
            success: function(res) {
              if (res.confirm) {
                //返回到tabbar，switchTab
                wx.switchTab({ url: '/pages/Home/Home' });
              }
            }
          })
        }else{
          wx.showToast({
            title: res.errmsg,//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
            icon: 'error', //图标，支持"success"、"loading"
            success: function (res) {
              if (res.errMsg == 'showToast:ok') {
                setTimeout(function () {
                  wx.switchTab({ url: '/pages/Home/Home' });
                }, 1000)
              }
            },
          })
        }
      })
  }
});
